package com.smartbus.bus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.smartbus.common.annotation.Log;
import com.smartbus.common.core.controller.BaseController;
import com.smartbus.common.core.domain.AjaxResult;
import com.smartbus.common.enums.BusinessType;
import com.smartbus.bus.domain.BusStations;
import com.smartbus.bus.domain.dto.DispatchStationDTO;
import com.smartbus.bus.service.IBusStationsService;
import com.smartbus.common.utils.poi.ExcelUtil;
import com.smartbus.common.core.page.TableDataInfo;

/**
 * 公交站点信息，存储所有公交站点的基础信息和GPS坐标Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/bus/stations")
public class BusStationsController extends BaseController
{
    @Autowired
    private IBusStationsService busStationsService;

    /**
     * 查询公交站点信息，存储所有公交站点的基础信息和GPS坐标列表
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusStations busStations)
    {
        startPage();
        List<BusStations> list = busStationsService.selectBusStationsList(busStations);
        return getDataTable(list);
    }

    /**
     * 导出公交站点信息，存储所有公交站点的基础信息和GPS坐标列表
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:export')")
    @Log(title = "公交站点信息，存储所有公交站点的基础信息和GPS坐标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusStations busStations)
    {
        List<BusStations> list = busStationsService.selectBusStationsList(busStations);
        ExcelUtil<BusStations> util = new ExcelUtil<BusStations>(BusStations.class);
        util.exportExcel(response, list, "公交站点信息，存储所有公交站点的基础信息和GPS坐标数据");
    }

    /**
     * 获取公交站点信息，存储所有公交站点的基础信息和GPS坐标详细信息
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(busStationsService.selectBusStationsById(id));
    }

    /**
     * 新增公交站点信息，存储所有公交站点的基础信息和GPS坐标
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:add')")
    @Log(title = "公交站点信息，存储所有公交站点的基础信息和GPS坐标", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusStations busStations)
    {
        return toAjax(busStationsService.insertBusStations(busStations));
    }

    /**
     * 修改公交站点信息，存储所有公交站点的基础信息和GPS坐标
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:edit')")
    @Log(title = "公交站点信息，存储所有公交站点的基础信息和GPS坐标", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusStations busStations)
    {
        return toAjax(busStationsService.updateBusStations(busStations));
    }

    /**
     * 删除公交站点信息，存储所有公交站点的基础信息和GPS坐标
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:remove')")
    @Log(title = "公交站点信息，存储所有公交站点的基础信息和GPS坐标", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(busStationsService.deleteBusStationsByIds(ids));
    }

    /**
     * 获取调度系统站点数据（包含地图坐标、状态等信息）
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:list')")
    @GetMapping("/dispatch")
    public AjaxResult getDispatchStations()
    {
        try {
            List<DispatchStationDTO> dispatchStations = busStationsService.getDispatchStations();
            return AjaxResult.success("获取调度站点数据成功", dispatchStations);
        } catch (Exception e) {
            logger.error("获取调度站点数据失败", e);
            return AjaxResult.error("获取调度站点数据失败: " + e.getMessage());
        }
    }

    /**
     * 获取站点实时状态
     */
    @PreAuthorize("@ss.hasPermi('bus:stations:list')")
    @GetMapping("/status")
    public AjaxResult getStationStatus()
    {
        try {
            List<DispatchStationDTO> stationStatus = busStationsService.getStationStatus();
            return AjaxResult.success("获取站点状态成功", stationStatus);
        } catch (Exception e) {
            logger.error("获取站点状态失败", e);
            return AjaxResult.error("获取站点状态失败: " + e.getMessage());
        }
    }
}
